@extends('admin.layouts.admin')
@section('content')
    <div class="layui-form" lay-filter="LAY-edit-forms" id="LAY-edit" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="display_name" lay-verify="required" value="{{$role->display_name}}"  placeholder="角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" value="{{$role->name}}" placeholder="标识" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限范围</label>
            <div class="layui-input-block">
                <table class="layui-table">
                    <tr class="b-group" style="background-color: transparent;">
                        <th >
                            <input  type="checkbox" name="" value="" title="权限配置"  lay-skin="primary" id="allChoose" lay-filter="allChoose">
                        </th>
                        <td></td>
                    </tr>
                    @foreach($permission as $v)
                        @if(empty($v['_data']))
                            <tr class="b-group" style="background-color: transparent;">
                                <th>
                                    <input  type="checkbox" name="permission_id[]" value="{{ $v['id'] }}" title="{{ $v['display_name'] }}" lay-skin="primary"  lay-filter="selectChoose" @if(in_array($v['id'], $roleHasPermission)) checked @endif >
                                </th>
                                <td></td>
                            </tr>
                        @else
                            <tr class="b-group" style="background-color: transparent;">
                                <th>
                                    <input  type="checkbox" name="permission_id[]" value="{{ $v['id'] }}" title="{{ $v['display_name'] }}" lay-skin="primary" lay-filter="selectChoose" @if(in_array($v['id'], $roleHasPermission)) checked @endif >
                                </th>
                                <td  class="b-child">
                                    @foreach($v['_data'] as $n)
                                        <table class="layui-table">
                                            <tr class="b-group" style="background-color: transparent;">
                                                <th >
                                                    <input type="checkbox" name="permission_id[]" value="{{ $n['id'] }}" title="{{ $n['display_name'] }}" lay-skin="primary" lay-filter="selectChoose"  @if(in_array($n['id'], $roleHasPermission)) checked @endif >
                                                </th>
                                                <td>
                                                    @if(!empty($n['_data']))
                                                        @foreach($n['_data'] as $c)
                                                            <input  type="checkbox" name="permission_id[]" value="{{ $c['id'] }}" title="{{ $c['display_name'] }}" lay-skin="primary"  @if(in_array($c['id'], $roleHasPermission)) checked @endif >
                                                        @endforeach
                                                    @endif
                                                </td>
                                            </tr>
                                        </table>
                                    @endforeach
                                </td>
                            </tr>
                        @endif
                    @endforeach

                </table>
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <input type="button" lay-submit lay-filter="LAY-edit-forms" id="LAY-edit-forms" value="确认">
        </div>
    </div>

@endsection
@section('scripts')
    <script>
        layui.config({
            base: '../' //静态资源所在路径
        }).use([ 'table', 'form'], function(){
            var $ = layui.$
                ,form = layui.form
            form.render('checkbox');
            //全选
            form.on('checkbox(allChoose)', function (data) {
                $(data.elem).parents('.b-group').parent().eq(0).find("input[type='checkbox']").prop('checked', $(data.elem).prop('checked'));
                form.render('checkbox');
            });
            //选择子项
            form.on('checkbox(selectChoose)', function (data) {
                //子孙项全选
                $(data.elem).parents('.b-group').eq(0).find("input[type='checkbox']").prop('checked', $(data.elem).prop('checked'));
                //父项选择
                $(data.elem).parents('.b-group').eq(1).find('th').eq(0).find("input[type='checkbox']").prop('checked', $(data.elem).prop('checked'))

                form.render('checkbox');
            });

            //判断是否在数组中
            var p_num = $(document).find("input[type=checkbox][name='permission_id[]']").length;
            var c_num = $(document).find("input[type=checkbox][name='permission_id[]']:checked").length;

            if(p_num == c_num){
                $(document).find("#allChoose").attr('checked',true);
            }

            form.render();
        });//use

    </script>
@stop
